<?php
/**
 *
 *  
 * Date: 2024/1/12
 * Email: < >
 */


namespace process;

use app\api\model\ChickenCards as ChickenCards;
use Workerman\Crontab\Crontab;
use app\api\model\UserUseCards as UserUseCards;
use app\api\model\Wallets as Wallets;


class InterestTask
{
    public function onWorkerStart()
    {

        // 每天零点05分更新利息 和 U蛋
        new Crontab('0 */1 * * * *', function () {
//            new Crontab('05 0 * * *', function () {
            //获取所有用户信息
            $ids = Wallets::pluck('wallet_user_id');
            $interest = ChickenCards::where('id',2)->value('interest');
            foreach ($ids as $id){
                $yiyou_eggs = UserUseCards::where('user_id',$id)->sum('done_eggs');
                $yiqu_eggs = Wallets::where('wallet_user_id',$id)->value('quchu_eggs');
                $producing_eggs = $yiyou_eggs-$yiqu_eggs;
                $interest_eggs=Wallets::where('wallet_user_id',$id)->value('interest_eggs');
                // 更新利息信息
                Wallets::where('wallet_user_id',$id)->update([
                    'interest_eggs'=>$producing_eggs*$interest/100+$interest_eggs
                ]);
            }
            //更新挖矿信息
            $wks = UserUseCards::pluck('id');
            foreach ($wks as $wk){
                $x1 = UserUseCards::where('id',$wk)->value('every_day_eggs');
                $x2 = UserUseCards::where('id',$wk)->value('done_eggs');
                $x3 = UserUseCards::where('id',$wk)->value('days');
                $x4 = UserUseCards::where('id',$wk)->value('is_del');
                $settingDay=ChickenCards::where('id',2)->value('setting_day');

                if($x3<$settingDay){
                    UserUseCards::where('id',$wk)->update([
                        'done_eggs'=>$x2+$x1,
                        'days'=>$x3+1,
                    ]);
                }else{
                    if($x4==0){
                        UserUseCards::where('id',$wk)->update([
                            'is_del'=>1,
                        ]);
                    }

                }
            }
        });

    }
}